package cn.ruanyun.backInterface.modules.business.sensitiveWords.serviceimpl;

import cn.ruanyun.backInterface.common.enums.SensitivityTypeEnum;
import cn.ruanyun.backInterface.common.utils.EmptyUtil;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.mapper.SensitiveWordsMapper;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.pojo.SensitiveWord;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.service.ISensitiveWordsService;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.utils.SensitiveWordEngine;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.utils.SensitiveWordInit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 敏感词接口实现
 *
 * @author tqr
 */
@Slf4j
@Service
@Transactional
public class ISensitiveWordsServiceImpl extends ServiceImpl<SensitiveWordsMapper, SensitiveWord> implements ISensitiveWordsService {

    @Resource
    private SensitiveWordsMapper sensitiveWordsMapper;

    @SuppressWarnings("rawtypes")
    @Override
    public Set<String> sensitiveWordFiltering(String text, SensitivityTypeEnum typeEnum) {
        // 初始化敏感词库对象
        SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
        // 从数据库中获取敏感词对象集合（调用的方法来自Dao层，此方法是service层的实现类）
        List<SensitiveWord> sensitiveWords = sensitiveWordsMapper.selectList(new LambdaQueryWrapper<SensitiveWord>().eq(EmptyUtil.isNotEmpty(typeEnum),SensitiveWord::getTypeEnum,typeEnum));
        // 构建敏感词库
        Map sensitiveWordMap = sensitiveWordInit.initKeyWord(sensitiveWords);
        // 传入SensitivewordEngine类中的敏感词库
        SensitiveWordEngine.sensitiveWordMap = sensitiveWordMap;
        // 得到敏感词有哪些，传入2表示获取所有敏感词
        Set<String> set = SensitiveWordEngine.getSensitiveWord(text, 2);

        return set;
    }
}